<?php
/**
 * @Project: api
 * @FileName: Response.php
 * @FilePath: D:\php\api\application\Response.php
 * @Author: zishuiliufeng
 * @Date:   2017-04-18 22:14:14
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2018-02-16 18:04:58
 */
namespace app;
class Response {
    const TYPE = "json";
    /**
     * [show 按综合方式输出通信数据]
     * @param  [integer] $code    [状态码]
     * @param  string $message [提示信息]
     * @param  array  $data    [数据]
     * @param  [string] $type    [输出数据类型]
     * @return [string]          [返回信息]
     */
    public static function show($code,$message = '',$data = array(),$type = ''){
        if(!is_numeric($code)){
            return '';
        }

        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );

        //是否允许跨域请求
        $allowweb = isset($_GET['allowweb'])?$_GET['allowweb']:'';

        if(isset($_GET['format'])){
            $type = !empty($_GET['format'])?$_GET['format']:'';
        }
        $type = !empty($type)?$type:self::TYPE;

        if($type == 'json'){
            //json输出
            self::jsonEncode($code,$message,$data,$allowweb);
            exit;
        }elseif($type == 'test'){
            //调试模式
            var_dump($result);
            exit;
        }elseif($type == 'xml'){
            self::xmlEncode($code,$message,$data);
            exit;
        }else{
            //TODO ELSE
        }
    }

    /**
     * [jsonEncode 按json方式输出通信数据]
     * @param  [integer] $code    [状态码]
     * @param  string $message [提示信息]
     * @param  array  $data    [数据]
     * @return [string]          [返回信息]
     */
    public static function jsonEncode($code,$message = '',$data = array(),$allowweb){
        
        if(!is_numeric($code)){
            return '';
        }

        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data 
        );

        if(!empty($allowweb)){
            header("Access-Control-Allow-Origin: *");
        }

        echo json_encode($result);
        exit;
    }
    /**
     * [xmlEncode 按xml方式输出通信数据]
     * @param  [integer] $code    [状态码]
     * @param  string $message [提示信息]
     * @param  array  $data    [数据]
     * @return [string]          [返回信息]
     */
    public static function xmlEncode($code,$message = '',$data = array()){

        if(!is_numeric($code)){
            return '';
        }
        
        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );

        header("Content-Type:text/xml");
        $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
        $xml .= "<root>\n";
        $xml .= self::xmlToEncode($result);
        $xml .= "</root>";

        echo $xml;
    }

    public static function xmlToEncode($data){
        $xml = "";
        if(is_array($data) && !empty($data))
        {
            foreach ($data as $key => $value) {
                $attr = "";
                if(is_numeric($key)){
                    $attr = " id='{$key}'";
                    $key = "item";
                }
                $xml .= "<{$key}{$attr}>";
                $xml .= is_array($value)?self::xmlToEncode($value):$value;
                $xml .= "</{$key}>\n";
            }
        }
        return $xml;
    }
}